INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include)
IF(WIN32)
  SET(EXTRA_SOURCES tpool_win.cc aio_win.cc)
ELSEIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  OPTION(WITH_URING "Require that io_uring be used" OFF)
  OPTION(WITH_LIBAIO "Require that libaio is used, unless uring is there" OFF)
  IF(WITH_URING)
    SET(URING_REQUIRED REQUIRED)
  ELSEIF(WITH_LIBAIO)
    SET(LIBAIO_REQUIRED REQUIRED)
  ENDIF()
  FIND_PACKAGE(URING QUIET ${URING_REQUIRED})
  IF(URING_FOUND)
    SET(URING_FOUND ${URING_FOUND} PARENT_SCOPE)
    SET(TPOOL_DEFINES "-DHAVE_URING" PARENT_SCOPE)
    ADD_DEFINITIONS(-DHAVE_URING)
    LINK_LIBRARIES(${URING_LIBRARIES})
    INCLUDE_DIRECTORIES(${URING_INCLUDE_DIRS})
    SET(EXTRA_SOURCES aio_liburing.cc)
    SET(CMAKE_REQUIRED_INCLUDES_SAVE ${CMAKE_REQUIRED_INCLUDES})
    SET(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES})
    SET(CMAKE_REQUIRED_INCLUDES ${URING_INCLUDE_DIRS})
    SET(CMAKE_REQUIRED_LIBRARIES ${URING_LIBRARIES})
    CHECK_SYMBOL_EXISTS(io_uring_mlock_size "liburing.h" HAVE_IO_URING_MLOCK_SIZE)
    SET(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_SAVE})
    SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_SAVE})
    IF(HAVE_IO_URING_MLOCK_SIZE)
      SET_SOURCE_FILES_PROPERTIES(aio_liburing.cc PROPERTIES COMPILE_FLAGS "-DHAVE_IO_URING_MLOCK_SIZE")
    ENDIF()
  ELSE()
    FIND_PACKAGE(LIBAIO QUIET ${LIBAIO_REQUIRED})
    IF(LIBAIO_FOUND)
      SET(TPOOL_DEFINES "-DLINUX_NATIVE_AIO" PARENT_SCOPE)
      ADD_DEFINITIONS(-DLINUX_NATIVE_AIO)
      INCLUDE_DIRECTORIES(${LIBAIO_INCLUDE_DIRS})
      LINK_LIBRARIES(${LIBAIO_LIBRARIES})
      SET(EXTRA_SOURCES aio_linux.cc)
    ENDIF()
  ENDIF()
ENDIF()

ADD_LIBRARY(tpool STATIC
  aio_simulated.cc
  tpool_structs.h
  CMakeLists.txt
  tpool.h
  tpool_generic.cc
  task_group.cc
  task.cc
  wait_notification.cc
  ${EXTRA_SOURCES}
)

IF(URING_FOUND)
  ADD_DEPENDENCIES(tpool GenError)
ENDIF()
